package spring.spring_blog.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import spring.spring_blog.common.utils.JwtUtil;
import spring.spring_blog.pojo.model.blogInfo;
import spring.spring_blog.pojo.request.addBlogReq;
import spring.spring_blog.pojo.request.loginReq;
import spring.spring_blog.pojo.request.registerReq;
import spring.spring_blog.pojo.request.updateBlogReq;
import spring.spring_blog.pojo.response.blogInfoResp;
import spring.spring_blog.pojo.response.loginInfoResp;
import spring.spring_blog.pojo.response.response;
import spring.spring_blog.pojo.model.userInfo;
import spring.spring_blog.pojo.response.userInfoResp;
import spring.spring_blog.server.Imp.blogServerImp;

import java.util.List;
import java.util.Map;

//tomorrow 搞下字符串过长了就给他分割了
@RequestMapping("blog")
@RestController
@Slf4j
public class blogController {

    @Autowired
    blogServerImp blogServer;

    /**
     * 登录接口
     * username password
     * 返回token来验证这个用户是登录了的和用户的id用来查询所有博客信息
     * 加密和加盐
     */
    @RequestMapping("login")
    public response login(@RequestBody @Validated loginReq l, HttpServletResponse resp) {

        userInfo u = blogServer.login(l.getUserName(), l.getPassword());

        if (u == null) {
            return response.fail("账号密码错误");
        }

        loginInfoResp req = new loginInfoResp();
        BeanUtils.copyProperties(u, req);

        String token = JwtUtil.getToken(Map.of("userInfo", u));

        req.setToken(token);

        log.info("token={}", token);

        return response.success("scc", req);
    }


    /**
     * 注册页面
     */
    @RequestMapping("register")
    public response register(@RequestBody @Validated registerReq l, HttpServletResponse resp) {

        return blogServer.register(l);
    }
    /**
     * 添加博客
     */
    @RequestMapping("addBlog")
    public response addBlog(@RequestBody @Validated addBlogReq l) {
        return blogServer.addBlog(l);
    }

    /**
     * 跟新博客
     */
    @RequestMapping("updateBlog")
    public response updateBlog(@RequestBody @Validated updateBlogReq updateReq) {
        return blogServer.updateBlog(updateReq);
    }

    /**
     * 获得用户信息
     */
    @RequestMapping("getUserInfo")
    public response getUserInfo(Integer userId) throws Exception {

        if (userId == null) {
            throw new Exception("类型错误");
        }
        userInfoResp userInfo = blogServer.getUserInfo(userId);

        return response.success("scc", userInfo);
    }

    /**
     *
     */
    @RequestMapping("getAllBlog")
    public response getAllBlog() {
        return response.success("scc", blogServer.getAllBlog());
    }

    /**
     * 获取所有blog信息锅通过id
     * id
     */
    @RequestMapping("getMyBlogInfo")
    public response getMyBlogInfo(HttpServletRequest req) {

        int userId = Integer.parseInt(req.getHeader("userId"));

        return response.success("scc", blogServer.getAllBlogInfo(userId));
    }


    /**
     * 获取详细信息通过blog的id
     * 这个右跑不起了，因为令牌没有传递
     */
    @RequestMapping("getBlogInfoDetail")
    public response getBlogInfoDetail(String blogId) {

        blogInfo b = blogServer.getBlogInfoDetail(Integer.parseInt(blogId));

        if (b == null) {
            return response.fail("博客不存在");
        }

        blogInfoResp resp = new blogInfoResp();
        BeanUtils.copyProperties(b, resp);

        return response.success("scc", resp);
    }

    /**
     * 删除blog 将delete_flag设置为1
     */
    @RequestMapping("delete")
    public response deleteBlogInfo(String blogId) {
        //可以使用异常来统一捕捉
        return response.success("scc", List.of(blogServer.deleteBlog(Integer.parseInt(blogId))));
    }

}
